﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace GoodMan.Infrastructure.Entities.Repository
{
    [Table("T_Information")]
    public class T_Information :IEntity<T_Information>
    {
        [Key]
        [Column(TypeName="int")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [Column(TypeName="nvarchar")]
        [StringLength(64)]
        public string Category { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string Title { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string RecruitDep { get; set; }
        public int RecruitNum { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string SalaryLevel { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(32)]
        public string SexReq { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(100)]
        public string WorkArea { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(512)]
        public string JobDescrption { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string LinkMan { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string Phone { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string EmailAdress { get; set; }
        public DateTime ExpireDate { get; set; }
        public int CTR { get; set; }
        public DateTime PubDate { get; set; }

        public override T_Information Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Information Find(System.Linq.Expressions.Expression<Func<T_Information, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Information.FirstOrDefault(where);
                if (item != null)
                {
                    this.Category = item.Category;
                    this.CTR = item.CTR;
                    this.EmailAdress = item.EmailAdress;
                    this.ExpireDate = item.ExpireDate;
                    this.Id = item.Id;
                    this.JobDescrption = item.JobDescrption;
                    this.LinkMan = item.LinkMan;
                    this.Phone = item.Phone;
                    this.PubDate = item.PubDate;
                    this.RecruitDep = item.RecruitDep;
                    this.RecruitNum = item.RecruitNum;
                    this.SalaryLevel = item.SalaryLevel;
                    this.SexReq = item.SexReq;
                    this.Title = item.Title;
                    this.WorkArea = item.WorkArea;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Information, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Information.Any(where);
            }
        }

        public override T_Information FindLast(System.Linq.Expressions.Expression<Func<T_Information, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Information.OrderByDescending(c=>c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.Category = item.Category;
                    this.CTR = item.CTR;
                    this.EmailAdress = item.EmailAdress;
                    this.ExpireDate = item.ExpireDate;
                    this.Id = item.Id;
                    this.JobDescrption = item.JobDescrption;
                    this.LinkMan = item.LinkMan;
                    this.Phone = item.Phone;
                    this.PubDate = item.PubDate;
                    this.RecruitDep = item.RecruitDep;
                    this.RecruitNum = item.RecruitNum;
                    this.SalaryLevel = item.SalaryLevel;
                    this.SexReq = item.SexReq;
                    this.Title = item.Title;
                    this.WorkArea = item.WorkArea;
                    return this;
                }
                return null;
            }
        }
    }
}
